# -*- coding:UTF-8 -*-

"""
这个文件用于数据库操作，连接数据库，增删改查
"""


import pymongo
import seccure


class db_class():
    def __init__(self):
        self.conn = pymongo.MongoClient('localhost',27017)
        self.user_db = self.conn.user_key
    def check_user(self,username):
        cu = self.user_db.user.find({"username":username})
        return cu.count()
    def add_user(self,username,pub):
        if(self.check_user(username)==0):
            data = {"username":username,"pub":pub.encode("base64")}
            self.user_db.user.insert(data)
            return 1
        else:
            return 0
    def change_password(self):
        pass
    def del_user(self,username):
        if(self.check_user(username)!=0):
            self.user_db.user.remove({"username":username})
            return 1
        else:
            return 0
    def get_uesr_pub(self,username):
        if (self.check_user(username) != 0):
            cu = self.user_db.user.find({"username": username})
            return cu
        else:
            return 0




